package cn.elead.chaos.sso.server.web.login.resolve;

import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import cn.elead.chaos.framework.util.EnumUtil;
import cn.elead.chaos.sso.server.enums.ResponseType;
import cn.elead.chaos.sso.server.web.login.vo.Login;

/**
 * 登录参数转换
 * 
 * @author Administrator
 *
 */
public class LgoinFormParamArgumentResolver implements HandlerMethodArgumentResolver {

	@Override
	public boolean supportsParameter(MethodParameter methodParameter) {
		return methodParameter.getParameterType().equals(Login.class);
	}

	@Override
	public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) {
		Login login = new Login();
		login.setUsername(nativeWebRequest.getParameter("username"));
		login.setPassword(nativeWebRequest.getParameter("password"));
		login.setIfRemember(Boolean.valueOf(nativeWebRequest.getParameter("ifRemember")));
		String response_type = nativeWebRequest.getParameter("response_type");
		login.setResponseType(EnumUtil.nameOf(ResponseType.class, response_type));
		login.setClientId(nativeWebRequest.getParameter("client_id"));
		login.setRedirectUri(nativeWebRequest.getParameter("redirect_uri"));
		return login;
	}

}
